﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Styx;
using Styx.Helpers;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Pathing;
using CommonBehaviors.Actions;
using Styx.TreeSharp;
using Action = Styx.TreeSharp.Action;
using Styx.Common;
using Styx.CommonBot;

namespace DWCC
{
    public partial class Warrior
    {

        protected Composite CreateDWCCBehavior()
        {
            return new PrioritySelector(
                new Decorator(ret => StyxWoW.Me.CurrentTarget == null || Me.CurrentTarget.IsDead || !Me.CurrentTarget.Attackable || Me.IsDead || Me.Mounted,
                new ActionIdle()),
            #region HealthRegen
                new Decorator(ret => (DunatanksSettings.Instance.useHealthStone && Me.HealthPercent <= DunatanksSettings.Instance.HealthStonePercent && HaveHealthStone() && HealthStoneNotCooldown()),
                new Action(ret => UseHealthStone())),
                new Decorator(ret => (DunatanksSettings.Instance.usePotion && Me.HealthPercent <= DunatanksSettings.Instance.PotionPercent && HaveHealthPotion() && HealthPotionReady()),
                new Action(ret => UseHealthPotion())),
            #endregion
            #region General
                StanceChange(),
                FaceTarget(),
                CastCharge(),
                CreateAutoAttack(),
                StopMoving(),
                MoveOutOfAoE(),
                HeroicLeap(),
                MoveToTarget(),
                UseRacialComposite(),
                AoEPummel(),
                UseTrinketOne(),
                UseTrinketTwo(),
            #endregion
            #region Interrupt
 CreateSpellCheckAndCast("Pummel", ret => DunatanksSettings.Instance.usePummel && Me.CurrentTarget.IsCasting && Me.CurrentTarget.CanInterruptCurrentSpellCast),
                    CreateSpellCheckAndCast("Disrupting Shout", ret => DunatanksSettings.Instance.usePummel && Me.CurrentTarget.IsCasting && Me.CurrentTarget.CanInterruptCurrentSpellCast),
            #endregion
            #region IV/VR
                    CreateSpellCheckAndCast("Impending Victory", ret => Me.HealthPercent < DunatanksSettings.Instance.IVHealth),
                    CreateSpellCheckAndCast("Victory Rush", ret => !SpellManager.HasSpell("Impending Victory") && Me.HealthPercent < 100),
            #endregion
            #region Cooldowns
                CreateSpellCheckAndCast("Die by the Sword", ret => !DunatanksSettings.Instance.useProt && Me.HealthPercent < DunatanksSettings.Instance.DbtSHealth),
            #region Fury
                CreateSpellCheckAndCast("Recklessness", ret => ((DunatanksSettings.Instance.FuryReckOnCD) || (DunatanksSettings.Instance.FuryReckOnBoss && Me.CurrentTarget.IsBoss)) && DunatanksSettings.Instance.useFury),
                CreateSpellCheckAndCast("Skull Banner", ret => (DunatanksSettings.Instance.FurySB && Me.HasAura("Recklessness")) && DunatanksSettings.Instance.useFury),
                CreateSpellCheckAndCast("Avatar", ret => DunatanksSettings.Instance.FuryAvaOnCD && Me.HasAura("Recklessness") && DunatanksSettings.Instance.useFury),
            #endregion
            #region MS
                CreateSpellCheckAndCast("Recklessness", ret => ((DunatanksSettings.Instance.ArmsReckOnCD) || (DunatanksSettings.Instance.ArmsReckOnBoss && Me.CurrentTarget.IsBoss)) && DunatanksSettings.Instance.useArms),
                CreateSpellCheckAndCast("Skull Banner", ret => (DunatanksSettings.Instance.ArmsSB && Me.HasAura("Recklessness")) && DunatanksSettings.Instance.useArms),
                CreateSpellCheckAndCast("Avatar", ret => DunatanksSettings.Instance.ArmsAvaOnCD && Me.HasAura("Recklessness") && DunatanksSettings.Instance.useArms),
            #endregion
            #region Prot
                CreateSpellCheckAndCast("Recklessness", ret => ((DunatanksSettings.Instance.ProtReckOnCD) || (DunatanksSettings.Instance.ProtReckOnBoss && Me.CurrentTarget.IsBoss)) && DunatanksSettings.Instance.useProt),
                CreateSpellCheckAndCast("Skull Banner", ret => (DunatanksSettings.Instance.ProtSB && Me.HasAura("Recklessness")) && DunatanksSettings.Instance.useProt),
                CreateSpellCheckAndCast("Avatar", ret => DunatanksSettings.Instance.ProtAvaOnCD && Me.HasAura("Recklessness") && DunatanksSettings.Instance.useProt),
                CreateSpellCheckAndCast("Shield Wall", ret => Me.HealthPercent < DunatanksSettings.Instance.SWHealth && DunatanksSettings.Instance.useSW && DunatanksSettings.Instance.useProt),
                CreateSpellCheckAndCast("Shield Block", ret => Me.HealthPercent < DunatanksSettings.Instance.SBHealth && DunatanksSettings.Instance.useSB && DunatanksSettings.Instance.useProt),
                CreateSpellCheckAndCast("Shield Barrier", ret => !Me.HasAura("Shield Barrier") && Me.HealthPercent < DunatanksSettings.Instance.SBarrHealth && DunatanksSettings.Instance.useSBarr && DunatanksSettings.Instance.useProt),
                CreateSpellCheckAndCast("Last Stand", ret => Me.HealthPercent < DunatanksSettings.Instance.LStandHealth && DunatanksSettings.Instance.useLStand && DunatanksSettings.Instance.useProt),
            #endregion
            #endregion
                FuryRotation(),
                ArmsRotation(),
                ProtRotation(),
                FuryLeveling(),
                ArmsLeveling(),
                ProtLeveling(),
                Lowbie(),
                MoveToTarget()
            );
        }

        public Composite FuryRotation()
        {
            return new Decorator(ret => Me.Level == 90 && DunatanksSettings.Instance.useFury,
                new PrioritySelector(
                    //CD's
                    CreateSpellCheckAndCast("Bloodbath", ret => Me.CurrentTarget.HasAura("Colossus Smash") && SpellManager.CanCast("Raging Blow")),
                    CreateSpellCheckAndCast("Berserker Rage", ret => !IsEnraged(Me) && Me.CurrentTarget.HasAura(86346) || (Me.HasAura("Recklessness") && !Me.HasAura("Raging Blow!"))),
                    //Rotation
                    CreateSpellCheckAndCast("Heroic Strike", ret => Me.CurrentTarget.HealthPercent >= 20 && Me.PowerPercent > 90 && detectAdds().Count == 1),
                    CreateSpellCheckAndCast("Cleave", ret => Me.CurrentTarget.HealthPercent >= 20 && Me.PowerPercent > 90 && detectAdds().Count == 2),
                    CreateSpellCheckAndCast("Whirlwind", ret => Me.CurrentTarget.HealthPercent >= 20 && Me.PowerPercent > 90 && detectAdds().Count > 2),
                    CreateSpellCheckAndCast("Bloodthirst", ret => true),
                    CreateSpellCheckAndCast("Colossus Smash", ret => true),
                    CreateSpellCheckAndCast("Execute", ret => true),
                    CreateSpellCheckAndCast("Raging Blow", ret => Me.CurrentTarget.HealthPercent > 20),
                    CreateSpellCheckAndCast("Dragon Roar", ret => Me.CurrentTarget.HealthPercent > 20),
                    CreateSpellCheckAndCast("Storm Bolt", ret => Me.CurrentTarget.HealthPercent > 20),
                    CreateSpellCheckAndCast("Shockwave", ret => Me.CurrentTarget.HealthPercent > 20),
                    CreateSpellCheckAndCast("Wild Strike", ret => Me.HasAura(46916) && Me.CurrentTarget.HealthPercent > 20),       //46916 = Bloodsurge
                    CreateSpellCheckAndCast("Heroic Strike", ret => (Me.PowerPercent >= 67 || (Me.HasAura("Deadly Calm") && Me.PowerPercent > 40)) && Me.CurrentTarget.HealthPercent > 20 && detectAdds().Count == 1),
                    CreateSpellCheckAndCast("Cleave", ret => (Me.PowerPercent >= 67 || (Me.HasAura("Deadly Calm") && Me.PowerPercent > 40)) && Me.CurrentTarget.HealthPercent > 20 && detectAdds().Count == 2),
                    CreateSpellCheckAndCast("Whirlwind", ret => (Me.PowerPercent >= 67 || (Me.HasAura("Deadly Calm") && Me.PowerPercent > 40)) && Me.CurrentTarget.HealthPercent > 20 && detectAdds().Count > 2),
                    CreateSpellCheckAndCast("Bladestorm", ret => detectAdds().Count > 2),
                    CreateSpellCheckAndCast("Impending Victory", ret => Me.CurrentTarget.HealthPercent > 20 || Me.HealthPercent < 40),
                    CreateSpellCheckAndCast("Heroic Throw", ret => Me.CurrentTarget.Distance > 10 && Me.CurrentTarget.HealthPercent > 20),
                    CreateSpellCheckAndCast("Deadly Calm", ret => Me.PowerPercent > 50 && Me.CurrentTarget.HealthPercent > 20),
                    CreateSpellCheckAndCast("Battle Shout", ret => Me.CurrentRage < 70)));
        }
        public Composite ArmsRotation()
        {
            return new Decorator(ret => Me.Level == 90 && DunatanksSettings.Instance.useArms,
                new PrioritySelector(
                    //CD's
                    CreateSpellCheckAndCast("Bloodbath", ret => Me.CurrentTarget.HasAura("Colossus Smash")),
                    CreateSpellCheckAndCast("Berserker Rage", ret => !IsEnraged(Me)),
                    CreateSpellCheckAndCast("Deadly Calm", ret => Me.CurrentRage >= 40),
                    CreateSpellCheckAndCast("Sweeping Strikes", ret => detectAdds().Count > 1),
                    //Rotation
                    CreateSpellCheckAndCast("Heroic Strike", ret => ((Stacks("Taste for Blood") >= DunatanksSettings.Instance.HSStacks || AuraTimeLeft("Taste for Blood") <= 2) && !(Me.CurrentTarget.HealthPercent < 20))),
                    CreateSpellCheckAndCast("Mortal Strike", ret => true),
                    CreateSpellCheckAndCast("Colossus Smash", ret => (!Me.CurrentTarget.HasAura("Colossus Smash") || (Me.CurrentTarget.HasAura("Colossus Smash") && Me.CurrentTarget.ActiveAuras["Colossus Smash"].TimeLeft.TotalSeconds <= 1.5))),
                    CreateSpellCheckAndCast("Execute", ret => true),
                    CreateSpellCheckAndCast("Dragon Roar", ret => true),
                    CreateSpellCheckAndCast("Storm Bolt", ret => true),
                    CreateSpellCheckAndCast("Overpower", ret => true),
                    CreateSpellCheckAndCast("Shockwave", ret => true),
                    CreateSpellCheckAndCast("Slam", ret => Me.CurrentRage >= 80 && Me.CurrentTarget.HealthPercent >= 20 && detectAdds().Count == 1),
                    CreateSpellCheckAndCast("Cleave", ret => Me.CurrentRage >= 80 && Me.CurrentTarget.HealthPercent >= 20 && detectAdds().Count > 1),
                    CreateSpellCheckAndCast("Heroic Strike", ret => Me.PowerPercent >= 95 && !(Me.CurrentTarget.HealthPercent < 20)),
                    CreateSpellCheckAndCast("Heroic Throw", ret => true),
                    CreateSpellCheckAndCast("Battle Shout", ret => Me.CurrentRage < 70 && !Me.CurrentTarget.HasAura("Colossus Smash")),
                    CreateSpellCheckAndCast("Bladestorm", ret => SpellManager.HasSpell("Colossus Smash") && SpellManager.Spells["Colossus Smash"].CooldownTimeLeft.TotalSeconds >= 5 && !Me.CurrentTarget.HasAura("Colossus Smash") && SpellManager.Spells["Mortal Strike"].CooldownTimeLeft.TotalSeconds >= 2 && Me.CurrentTarget.HealthPercent >= 20 || detectAdds().Count > 1),
                    CreateSpellCheckAndCast("Slam", ret => Me.CurrentTarget.HealthPercent >= 20),
                    CreateSpellCheckAndCast("Impending Victory", ret => Me.CurrentTarget.HealthPercent >= 20),
                    CreateSpellCheckAndCast("Battle Shout", ret => Me.CurrentRage < 70)));
        }
        public Composite ProtRotation()
        {
            return new Decorator(ret => Me.Level == 90 && DunatanksSettings.Instance.useProt,
                new PrioritySelector(
                    CreateSpellCheckAndCast("Impending Victory", ret => Me.HealthPercent < 65),
                    CreateSpellCheckAndCast("Taunt", ret => DunatanksSettings.Instance.useTaunt && (!Me.CurrentTarget.Aggro && Me.CurrentTarget.GotTarget && !TargetTargetIsTank())),
                    CreateSpellCheckAndCast("Thunder Clap", ret => true),
                    CreateSpellCheckAndCast("Shockwave", ret => true),
                    CreateSpellCheckAndCast("Dragon Roar", ret => detectAdds().Count > 2 || Me.CurrentTarget.IsBoss),
                    CreateSpellCheckAndCast("Berserker Rage", ret => true),
                    CreateSpellCheckAndCast("Deadly Calm", ret => Me.CurrentTarget.IsBoss || detectAdds().Count > 2),
                    CreateSpellCheckAndCast("Cleave", ret => detectAdds().Count > 1 && Me.HasAura("Ultimatum") || detectAdds().Count > 1 && Me.CurrentRage > 70),
                    CreateSpellCheckAndCast("Heroic Strike", ret => Me.HasAura("Ultimatum")),
                    CreateSpellCheckAndCast("Execute", ret => Me.CurrentTarget.HealthPercent < 20 && detectAdds().Count < 3),
                    CreateSpellCheckAndCast("Shield Slam", ret => true),
                    CreateSpellCheckAndCast("Revenge", ret => true),
                    CreateSpellCheckAndCast("Thunder Clap", ret => true),
                    CreateSpellCheckAndCast("Demoralizing Shout", ret => true),
                    CreateSpellCheckAndCast("Devastate", ret => true)));
        }
        public Composite FuryLeveling()
        {
            return new Decorator(ret => Me.Level >= 10 && Me.Level < 90 && DunatanksSettings.Instance.useFury,
                new PrioritySelector(
                    CreateSpellCheckAndCast("Berserker Rage", ret => (!IsEnraged(Me) || Me.CurrentTarget.HealthPercent >= 20)),
                    CreateSpellCheckAndCast("Deadly Calm", ret => Me.CurrentRage >= 40),
                    CreateSpellCheckAndCast("Heroic Strike", ret => Me.CurrentTarget.HealthPercent >= 20 && Me.PowerPercent > 95 && detectAdds().Count == 1),
                    CreateSpellCheckAndCast("Cleave", ret => Me.CurrentTarget.HealthPercent >= 20 && Me.PowerPercent > 95 && detectAdds().Count == 2),
                    CreateSpellCheckAndCast("Whirlwind", ret => Me.CurrentTarget.HealthPercent >= 20 && Me.PowerPercent > 95 && detectAdds().Count > 2),
                    CreateSpellCheckAndCast("Bloodthirst", ret => true),
                    CreateSpellCheckAndCast("Colossus Smash", ret => true),
                    CreateSpellCheckAndCast("Execute", ret => true),
                    CreateSpellCheckAndCast("Raging Blow", ret => true),
                    CreateSpellCheckAndCast("Shockwave", ret => true),
                    CreateSpellCheckAndCast("Dragon Roar", ret => true),
                    CreateSpellCheckAndCast("Bladestorm", ret => detectAdds().Count > 1),
                    CreateSpellCheckAndCast("Heroic Strike", ret => Me.CurrentTarget.HealthPercent >= 20 && detectAdds().Count == 1),
                    CreateSpellCheckAndCast("Cleave", ret => Me.CurrentTarget.HealthPercent >= 20 && detectAdds().Count == 2),
                    CreateSpellCheckAndCast("Whirlwind", ret => Me.CurrentTarget.HealthPercent >= 20 && detectAdds().Count > 2),
                    CreateSpellCheckAndCast("Wild Strike", ret => Bloodsurge()),
                    CreateSpellCheckAndCast("Battle Shout", ret => Me.CurrentRage < 70 && !Me.CurrentTarget.HasAura("Colossus Smash")),
                    CreateSpellCheckAndCast("Bladestorm", ret => detectAdds().Count > 1),
                    CreateSpellCheckAndCast("Battle Shout", ret => Me.CurrentTarget.HealthPercent < 70)));
        }
        public Composite ArmsLeveling()
        {
            return new Decorator(ret => Me.Level >= 10 && Me.Level < 90 && DunatanksSettings.Instance.useArms,
                new PrioritySelector(
                    CreateSpellCheckAndCast("Berserker Rage", ret => Me.CurrentTarget.HealthPercent >= 20 && !IsEnraged(Me)),
                    CreateSpellCheckAndCast("Sweeping Strikes", ret => detectAdds().Count > 1),
                    CreateSpellCheckAndCast("Mortal Strike", ret => true),
                    CreateSpellCheckAndCast("Colossus Smash", ret => true),
                    CreateSpellCheckAndCast("Execute", ret => true),
                    CreateSpellCheckAndCast("Bladestorm", ret => detectAdds().Count > 1),
                    CreateSpellCheckAndCast("Overpower", ret => Me.CurrentTarget.HealthPercent >= 20),
                    CreateSpellCheckAndCast("Slam", ret => Me.CurrentTarget.HealthPercent >= 20 && detectAdds().Count == 1),
                    CreateSpellCheckAndCast("Cleave", ret => Me.CurrentTarget.HealthPercent >= 20 && detectAdds().Count > 1),
                    CreateSpellCheckAndCast("Thunder Clap", ret => Me.CurrentTarget.HealthPercent >= 20 && detectAdds().Count > 3),
                    CreateSpellCheckAndCast("Dragon Roar", ret => true),
                    CreateSpellCheckAndCast("Shockwave", ret => true),
                    CreateSpellCheckAndCast("Heroic Strike", ret => Me.CurrentTarget.HealthPercent >= 20 && !SpellManager.HasSpell("Slam") || Stacks("Taste for Blood") >= 1),
                    CreateSpellCheckAndCast("Heroic Throw", ret => Me.CurrentTarget.HealthPercent >= 20),
                    CreateSpellCheckAndCast("Battle Shout", ret => Me.CurrentTarget.HealthPercent >= 20 && Me.CurrentRage < 70)));
        }
        public Composite ProtLeveling()
        {
            return new Decorator(ret => Me.Level >= 10 && Me.Level < 90 && DunatanksSettings.Instance.useProt,
                new PrioritySelector(
                    CreateSpellCheckAndCast("Spell Reflection", ret => Me.CurrentTarget.IsCasting && !Me.CurrentTarget.CastingSpell.IsFunnel && Me.CurrentTarget.CurrentTargetGuid == Me.Guid),
                    CreateSpellCheckAndCast("Demoralizing Shout", ret => !Me.CurrentTarget.HasAura("Demoralizing Shout")),
                    CreateSpellCheckAndCast("Thunderclap", ret => true),
                    CreateSpellCheckAndCast("Cleave", ret => detectAdds().Count > 1 && (Me.HasAura("Ultimatum") || Me.CurrentRage > 90)),
                    CreateSpellCheckAndCast("Taunt", ret => DunatanksSettings.Instance.useTaunt && (!Me.CurrentTarget.Aggro && Me.CurrentTarget.GotTarget && !TargetTargetIsTank())),
                    CreateSpellCheckAndCast("Shield Slam", ret => true),
                    CreateSpellCheckAndCast("Revenge", ret => true),
                    CreateSpellCheckAndCast("Devastate", ret => true),
                    CreateSpellCheckAndCast("Thunder Clap", ret => (!Me.CurrentTarget.HasAura("Weakened Blows") || Me.CurrentTarget.ActiveAuras["Weakened Blows"].TimeLeft.TotalSeconds < 2)),
                    CreateSpellCheckAndCast("Heroic Strike", ret => (Me.HasAura("Ultimatum") || Me.CurrentRage > 90)),
                    CreateSpellCheckAndCast("Commanding Shout", ret => Me.CurrentRage < 70)));
        }
        public Composite Lowbie()
        {
            return new Decorator(ret => Me.Level < 10,
                new PrioritySelector(
                    CreateSpellCheckAndCast("Execute", ret => true),
                    CreateSpellCheckAndCast("Heroic Strike", ret => true)));
        }
        public Composite PvP()
        {
            return new Decorator(ret => Me.Level  == 90 && DunatanksSettings.Instance.usePvPRotation,
                new PrioritySelector(
                    CreateSpellCheckAndCast("Piercing Howl", ret => detectAdds().Count > 1 && !IsSlowed(Me.CurrentTarget)),
                    CreateSpellCheckAndCast("Staggering Howl", ret => detectAdds().Count > 1 && !IsSlowed(Me.CurrentTarget)),
                    CreateSpellCheckAndCast("Hamstring", ret => !IsSlowed(Me.CurrentTarget))));
        }
    }
}